/**
 * 
 */
package edu.gtcfla.km.security.service.impl;

import java.util.Collection;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.biz.account.User;

import edu.gtcfla.km.security.api.Account;
import edu.gtcfla.km.security.api.SubjectManager;

/**
 * @author Bill Vison
 *
 */
public class SubjectManagerI implements SubjectManager {
	
	private static Logger logger = LoggerFactory.getLogger( SubjectManagerI.class ); 


	/* (non-Javadoc)
	 * @see org.vsg.biz.appaccount.AccountManager#getCurrnetAccount()
	 */
	@Override
	public Account getCurrentAccount() {
		
		Subject currentUser = SecurityUtils.getSubject();
		Collection pricipalColl = currentUser.getPrincipals().fromRealm(edu.gtcfla.km.security.api.AccountPrincipalConstant.ACCOUNT);

		User user = (User)pricipalColl.iterator().next();		
		
		SubjectAccountProxy proxy = new SubjectAccountProxy();
		proxy.setId( user.getId() );
		proxy.setLoginName( user.getName() );

		return proxy;
	}

	@Override
	public String getCurrentAccountId() {
		// TODO Auto-generated method stub
		Subject currentUser = SecurityUtils.getSubject();

		
		PrincipalCollection princColl = currentUser.getPrincipals();
		
		String userId = null;
		if (princColl != null) {
			Collection pricipalColl = princColl.fromRealm(edu.gtcfla.km.security.api.AccountPrincipalConstant.REALMNAME_USER_ID);
			userId = pricipalColl.iterator().next().toString();	

		} else {

			logger.warn("Could not found Principal Collection Form Session Id " + currentUser.getSession().getId());
			
		}
		return userId;
	}

}
